package JAVA300.第十一章多线程;

/**
 * 学习可重入锁-不可重用锁测试
 */
public class thread_19_others_7reEntryLock1 {

    Lock lock = new Lock();
    public void a (){
        lock.lock();
        doSomthing();
        lock.unlock();

    }

    //不可重入锁
    public void doSomthing(){
        lock.lock();
        //
        lock.unlock();

    }


    public static void main(String[] args) {

        thread_19_others_7reEntryLock1 entry = new thread_19_others_7reEntryLock1();
        entry.a();

    }

}

class Lock{
    //是否占用
    private boolean isLocked= false;

    //使用锁

    public synchronized void lock(){
        while (isLocked){
            try {
                wait();//锁被占用 等待
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        isLocked = true;

    }
    //解锁
    public synchronized void unlock(){
        isLocked = false;
        notifyAll();//唤醒


    }

}
